<?php

class DMD_Resources_EntityManager extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * @var Doctrine\ORM\EntityManager
     */
    protected $_em;

    public function init()
    {
        $this->_em = $this->getEntityManager();
        return $this->_em;
    }

    public function getEntityManager()
    {
        $options = $this->getOptions(); 
        $config = new \Doctrine\ORM\Configuration();
        $config->setProxyDir($options['metadata']['proxyDir']);
        $config->setProxyNamespace('Proxy');
        $config->setAutoGenerateProxyClasses((APPLICATION_ENV == 'development'));
        $driverImpl = $config->newDefaultAnnotationDriver($options['metadata']['entityDir']);
        $config->setMetadataDriverImpl($driverImpl);
        $cache = new Doctrine\Common\Cache\ArrayCache();
        $config->setMetadataCacheImpl($cache);
        $config->setQueryCacheImpl($cache);

        $evm = new Doctrine\Common\EventManager();
        $em = Doctrine\ORM\EntityManager::create($options['db'],$config,$evm);

        return $em;
    }
}
